package com.cpi.cornpp.version.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.corntypes.services.CornTypesService;
import com.cpi.cornpp.version.exception.VersionException;
import com.cpi.cornpp.version.services.VersionService;
import com.cs.common.utils.NumberUtil;

public class AdjustValuesToVersionMonths extends Action {
	private final static String DEFAULT = "default";//$NON-NLS-1$
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String VERSION_ID_LABEL = "versionId";//$NON-NLS-1$
	private static final String MONTH_PERIOD_VERSION_VIEWS = "periodosVersion";//$NON-NLS-1$
	private static final String MONTH_PERIOD_VERSION_ALL_VIEWS = "periodosAllVersion";//$NON-NLS-1$
	private static final String MONTH_PERIOD_VERSION_TOTAL = "totalPeriods";//$NON-NLS-1$
	private static final String MONTH_PERIOD_VERSION_TOTAL_WITH_CORN = "totalPeriodsWithCorn";//$NON-NLS-1$
	private static final String MONTH_PERIOD_VERSION_TOTAL_SIZE = "totalSize";//$NON-NLS-1$
	private static final String MONTH_PERIOD_VERSION_TOTAL_SIZE_WITH_CORN = "totalSizeWithCorn";//$NON-NLS-1$
	private final static String CORN_TYPE_VIEWS = "cornTypeViews";//$NON-NLS-1$
	private final static String MONTH_PERIOD_VERSION_SIZE_TD = "totalSizeTD";//$NON-NLS-1$
	private final static String PERIOD_SELECTED_ID = "periodId";//$NON-NLS-1$
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws VersionException {
		HttpSession httpSession = null;
		CornTypesService cornTypesService = null;
		List cornTypeViews = null;
		VersionService versionService = null;
		int versionId = -1;
		List monthPeriodVersionViews = null;
		List monthPeriodVersionAllViews = null;
		String periodId = null;
		try{
			httpSession = request.getSession();
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID_LABEL));
			
			cornTypesService = new CornTypesService();
			cornTypeViews = cornTypesService.getListCornTypes();
			
			versionService = new VersionService();
			periodId = request.getParameter(PERIOD_SELECTED_ID);
			if( periodId == null || Integer.parseInt(periodId) == 0 ){
				periodId = StringUtils.EMPTY;
				monthPeriodVersionViews = versionService.getVersionsPeriods(versionId);
				request.setAttribute(MONTH_PERIOD_VERSION_ALL_VIEWS, monthPeriodVersionViews);
			}else{
				monthPeriodVersionViews = versionService.getVersionsPeriods(versionId, Integer.parseInt(periodId));
				monthPeriodVersionAllViews = versionService.getVersionsPeriods(versionId);
				request.setAttribute(MONTH_PERIOD_VERSION_ALL_VIEWS, monthPeriodVersionAllViews);
			}
			if( monthPeriodVersionViews != null ){
				request.setAttribute(MONTH_PERIOD_VERSION_TOTAL, new Integer( monthPeriodVersionViews.size()+1 ));
				request.setAttribute(MONTH_PERIOD_VERSION_TOTAL_SIZE, new Integer( (monthPeriodVersionViews.size()+1)*150 ));
				request.setAttribute(MONTH_PERIOD_VERSION_TOTAL_SIZE_WITH_CORN, new Integer( ( monthPeriodVersionViews.size()*(150 + (40 * cornTypeViews.size())) ) + 150 ));
				request.setAttribute(MONTH_PERIOD_VERSION_SIZE_TD, new Integer( 150 + (40 * cornTypeViews.size() ) ) );
				request.setAttribute(MONTH_PERIOD_VERSION_TOTAL_WITH_CORN, new Integer( (monthPeriodVersionViews.size()*cornTypeViews.size())+1 ));
				request.setAttribute(MONTH_PERIOD_VERSION_VIEWS, monthPeriodVersionViews);
				request.setAttribute(PERIOD_SELECTED_ID, periodId);
			}
			request.setAttribute(CORN_TYPE_VIEWS, cornTypeViews);
		}catch (VersionException versionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
			throw versionException;
		}catch (Exception exception){
			VersionException versionException = null;
			exception.printStackTrace(System.out);
			versionException= new VersionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
			throw versionException;
		}
		return mapping.findForward(DEFAULT);
	}
}